package com.aaron.gesturehelper;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

	private static final String TAG = "MyService";
	private ToolBoxViewManager mView; 
	private BroadcastReceiver mReceiver;
	
	public void onCreate() {
		Log.i(TAG,"onCreate");
		super.onCreate();
		IntentFilter filter = new IntentFilter(Contants.INTENT_CONFIGURATION_CHANGED);
		mReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				String action  = intent.getAction();
				if(mView != null) {
					if(action.equals(Contants.INTENT_CONFIGURATION_CHANGED)) {
						if(intent.getBooleanExtra(MyConfiguration.GESTURE, false)) {
							mView.loadGestureLibrary();
						}
						if(intent.getBooleanExtra(MyConfiguration.GESTURE_COLOR, false)) {
							mView.resetGestureViewPaintColor();
						}
						if(intent.getBooleanExtra(MyConfiguration.GESTUREAREA_COLOR, false)) {
							mView.resetGestureViewBackground();
						}
						if(intent.getBooleanExtra(MyConfiguration.GESTUREAREA_EXPAND_TIME, false)) {
							mView.loadExpandTime();
						}
						if(intent.getBooleanExtra(MyConfiguration.VIBRATE, false)) {
							mView.loadVibrate();
						}
					}
				}
			}
		};
		registerReceiver(mReceiver, filter);
		showToolBox();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
//	@Override
//	public int onStartCommand(Intent intent, int flags, int startId) {
//		Log.i(TAG,"onStartCommand");
//		showToolBox();
//		return Service.START_STICKY;
//	}
	
	public void onConfigurationChanged(Configuration config) {
		if(mView != null) {
			mView.changeOrientation(config.orientation);
		}
		super.onConfigurationChanged(config);
	}
	
	private void showToolBox() {
		Log.i(TAG,"showToolBox");
		if(mView == null) {
			try {
				startKeyInjectServer();
			} catch (IOException e) {
				e.printStackTrace();
			}
			mView = new ToolBoxViewManager(this);
		}
	}
	
	private void startKeyInjectServer() throws IOException {
		Process process = null;
		DataOutputStream os = null;
		
		String path = new File(getFilesDir(),"CommandHandleServer.jar").getAbsolutePath();
		process = Runtime.getRuntime().exec("su");
		os = new DataOutputStream(process.getOutputStream());
		os.writeBytes("export CLASSPATH="+path+"\n");
		os.writeBytes("exec app_process /system/bin com.aaron.gesturehelper.commandserver.CommandHandleServer\n");
		os.flush();
	}

	public void onDestroy() {
		Log.i(TAG,"onDestroy");
		if(mView != null) {
			mView.dismiss();
			mView = null;
			MyConfiguration.getInstance(this).setBoolean(MyConfiguration.ENABLE, false);
		}
		unregisterReceiver(mReceiver);
		mReceiver = null;
		super.onDestroy();
	}
}
